// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.profanity;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import zombie.profanity.locales.Locale;
import zombie.profanity.locales.LocaleChinese;
import zombie.profanity.locales.LocaleEnglish;
import zombie.profanity.locales.LocaleGerman;

public class ProfanityFilter {
    public static boolean DEBUG = false;
    private Map<String, Locale> locales = new HashMap();
    private Locale locale;
    private Locale localeDefault;
    private Pattern prePattern;
    private boolean enabled = true;
    public static String LOCALES_DIR = "media" + File.separator + "profanity" + File.separator + "locales" + File.separator;
    private static ProfanityFilter instance;

    public static ProfanityFilter getInstance() {
        if (instance == null) {
            instance = new ProfanityFilter();
        }

        return instance;
    }

    private ProfanityFilter() {
        this.addLocale(new LocaleEnglish("EN"), true);
        this.addLocale(new LocaleGerman("GER"));
        this.addLocale(new LocaleChinese("CHIN"));
        this.prePattern = Pattern.compile("(?<spaced>(?:(?:\\s|\\W)[\\w\\$@](?=\\s|\\W)){2,20})|(?<word>[\\w'\\$@_-]+)");
    }

    public static void printDebug(String str) {
        if (DEBUG) {
            System.out.println(str);
        }
    }

    public void enable(boolean b) {
        this.enabled = b;
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public int getFilterWordsCount() {
        return this.locale != null ? this.locale.getFilterWordsCount() : 0;
    }

    public void addLocale(Locale l) {
        this.addLocale(l, false);
    }

    public void addLocale(Locale l, boolean setDefault) {
        this.locales.put(l.getID(), l);
        if (setDefault) {
            this.locale = l;
            this.localeDefault = l;
        }
    }

    public Locale getLocale() {
        return this.locale;
    }

    public void addWhiteListWord(String word) {
        if (this.locale != null) {
            this.locale.addWhiteListWord(word);
        }
    }

    public void removeWhiteListWord(String word) {
        if (this.locale != null) {
            this.locale.removeWhiteListWord(word);
        }
    }

    public void addFilterWord(String word) {
        if (this.locale != null) {
            this.locale.addFilterWord(word);
        }
    }

    public void removeFilterWord(String word) {
        if (this.locale != null) {
            this.locale.removeFilterWord(word);
        }
    }

    public void setLocale(String _locale) {
        if (this.locales.containsKey(_locale)) {
            this.locale = (Locale)this.locales.get(_locale);
        } else {
            this.locale = this.localeDefault;
        }
    }

    public String filterString(String str) {
        if (this.enabled && this.locale != null && str != null && this.locale.getFilterWordsCount() > 0) {
            try {
                StringBuffer stringBuffer = new StringBuffer();
                Matcher matcher = this.prePattern.matcher(str);

                while (matcher.find()) {
                    if (matcher.group("word") != null) {
                        matcher.appendReplacement(stringBuffer, Matcher.quoteReplacement(this.locale.filterWord(matcher.group("word"), true)));
                    } else if (matcher.group("spaced") != null) {
                        matcher.appendReplacement(
                            stringBuffer, Matcher.quoteReplacement(" " + this.locale.filterWord(matcher.group("spaced").replaceAll("\\s+", "")))
                        );
                    }
                }

                matcher.appendTail(stringBuffer);
                return stringBuffer.toString();
            } catch (Exception exception) {
                System.out.println("Profanity failed for: " + str);
            }
        }

        return str;
    }

    public String validateString(String str) {
        return this.validateString(str, true, true, true);
    }

    public String validateString(String str, boolean includePhonetics, boolean includeContains, boolean includeSpacedWords) {
        if (this.enabled && this.locale != null && str != null && this.locale.getFilterWordsCount() > 0) {
            try {
                Object object = null;
                boolean _boolean = false;
                StringBuilder stringBuilder = new StringBuilder();
                Matcher matcher = this.prePattern.matcher(str);

                while (matcher.find()) {
                    if (includePhonetics && matcher.group("word") != null) {
                        object = this.locale.validateWord(matcher.group("word"), includeContains);
                        if (object != null) {
                            if (_boolean) {
                                stringBuilder.append(", ");
                            }

                            stringBuilder.append((String)object);
                            _boolean = true;
                        }
                    } else if (includeSpacedWords && matcher.group("spaced") != null) {
                        object = this.locale.validateWord(matcher.group("spaced").replaceAll("\\s+", ""), false);
                        if (object != null) {
                            if (_boolean) {
                                stringBuilder.append(", ");
                            }

                            stringBuilder.append((String)object);
                            _boolean = true;
                        }
                    }
                }

                return _boolean ? stringBuilder.toString() : null;
            } catch (Exception exception) {
                System.out.println("Profanity validate string failed for: " + str);
                exception.printStackTrace();
            }
        }

        return "Failed to parse string :(.";
    }
}
